#include "umltextchilditem.h"

UmlTextChildItem::UmlTextChildItem(QGraphicsItem *parent) : QGraphicsTextItem(parent)
{
    QTextOption option;
    option.setWrapMode(QTextOption::WordWrap);

    this->setAcceptHoverEvents(true);
    this->setFlags(QGraphicsItem::ItemIsSelectable);

    this->document()->setDefaultTextOption(option);
}

void UmlTextChildItem::setWidth(qreal width)
{
    this->setTextWidth(width);
    this->_width = width;
}

QRectF UmlTextChildItem::boundingRect() const
{
    QRectF bBox = QGraphicsTextItem::boundingRect();

    if (this->_width > 0) {
        bBox.setWidth(this->_width);
    }

    return bBox;
}

QSizeF UmlTextChildItem::size() const
{
    return this->document()->size();
}

void UmlTextChildItem::setIsHovered(bool isHovered)
{
    if (this->_isHovered != isHovered) {
        this->_isHovered = isHovered;
    }

    this->update();
}

void UmlTextChildItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        this->openEditor();
    }
}

void UmlTextChildItem::updateUmlText()
{
    this->_umlText = this->_name;
}
